//
//  HotPresenterHomepageController.m
//  Everest
//
//  Created by Liu Chengbo on 14/12/23.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "HotPresenterHomepageController.h"
#import "UIImageView+WebCache.h"
#import "NetHandler.h"
#import "FindHotPresenter.h"
#import "UIButton+WebCache.h"
#import "FindHotPresenterViewController.h"
#import "FindHotAlbum.h"
#import "DownloadCell.h"
#import "HotPresent.h"
#import "HotCell.h"
#import "UIColor+AddColor.h"
#import "PlayViewController.h"
#import "AlbumViewController.h"
#import "ClassifyDetail.h"
#import "Album.h"
#import "AllAlbumViewController.h"
#import "FindMayBeLikeUser.h"
#import "MJRefresh.h"
#import "FilesDownManage.h"
#import "NetHandleData.h"
#import "JingRoundView.h"
#import "AppDelegate.h"
#import "WeiboSDK.h"
#import "FocusFetail.h"
#import "AvReadioPlayer.h"
#define kPage 1
#define REDIRECTURI @"http://www.sharesdk.cn"

@interface HotPresenterHomepageController () <UITableViewDataSource, UITableViewDelegate, JingRoundViewDelegate>
@property (nonatomic, retain) UITableView *personTableView;
@property (nonatomic, retain) UIButton *personButton;
@property (nonatomic, retain) UILabel *personNameLabel;
@property (nonatomic, retain) UILabel *personDescribeLabel;
@property (nonatomic, retain) UIImageView *personBackgroundImageView;
@property (nonatomic, retain) UIButton *attentionButton;
@property (nonatomic, retain) UIButton *hadAttentionButton;
@property (nonatomic, retain) UIButton *attentionCountButton;
@property (nonatomic, retain) UIButton *fansCountButton;
@property (nonatomic, retain) UIButton *favouredCountButton;
@property (nonatomic, retain) UILabel *attentionLabel;
@property (nonatomic, retain) UILabel *fansLabel;
@property (nonatomic, retain) UILabel *favouredLabel;
@property (nonatomic, retain) FindHotPresenter *hot;
@property (nonatomic, retain) NSMutableArray *albumArr;
@property (nonatomic, retain) NSMutableArray *hotPresentArr;
@property (nonatomic, retain) NSString *firstSectionStr;
@property (nonatomic, retain) NSString *secondSectionStr;
@property (nonatomic, retain) UIButton *favourButton;
@property (nonatomic, retain) UIButton *talkButton;
@property (nonatomic, retain) UIButton *downLoadButton;
@property (nonatomic, retain) HotPresent *hotP;
@property (nonatomic, assign) int day;
@property (nonatomic, retain) NSNumber *paId;
@property (nonatomic, retain) UIView *topView;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, retain) JingRoundView *roundView;
@property (nonatomic, retain) UIView *backView;
@property (nonatomic, assign, getter=isClicked) BOOL click;
@end

@implementation HotPresenterHomepageController
- (void)dealloc
{
    [_roundView release];
    [_headView release];
    [_hadAttentionButton release];
    [_topView release];
    [_personTableView release];
    [_personButton release];
    [_personNameLabel release];
    [_personDescribeLabel release];
    [_personBackgroundImageView release];
    [_attentionButton release];
    [_attentionCountButton release];
    [_fansCountButton release];
    [_favouredCountButton release];
    [_attentionLabel release];
    [_fansLabel release];
    [_favouredLabel release];
    [_albumArr release];
    [_hotPresentArr release];
    [_firstSectionStr release];
    [_secondSectionStr release];
    [_hotP release];
    [_talkButton release];
    [_downLoadButton release];
    [_favourButton release];
    [_paId release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        HotPresent *hott = [[HotPresent alloc]init];
        self.hotPresentArr = [NSMutableArray arrayWithObjects:hott, nil];
        self.page = 1;
    }
    return self;
}
- (void)handleData
{
    // 请求1
    NSString *strPerson = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?device=iPhone&toUid=%@", self.mayBeLikeUser.uid];
    [NetHandleData getDataWithUrl:strPerson completion:^(NSData *data) {
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        self.hot = [[FindHotPresenter alloc] initWithDictionary:dic];
        
//        self.paId = [dic objectForKey:@"pageId"];
        
        // 赋值
        [self.personButton setImageWithURL:[NSURL URLWithString:self.hot.mobileSmallLogo]];
        self.personNameLabel.text = self.hot.nickname;
        NSLog(@"%@", self.hot.nickname);
        self.attentionLabel.text = [NSString stringWithFormat:@"%@", self.hot.followings];
        // good!
        float fansInt = [self.hot.followers floatValue] / 10000;
        self.fansLabel.text = [NSString stringWithFormat:@"%.1f万", fansInt];
        
        self.favouredLabel.text = [NSString stringWithFormat:@"%@", self.hot.favorites];
        self.personDescribeLabel.text = self.hot.personDescribe;
        [self.personBackgroundImageView setImageWithURL:[NSURL URLWithString:self.hot.backgroundLogo]];
    }];
    // 请求2
    NSString *strAlbum = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%@/1/2?device=iPhone", self.mayBeLikeUser.uid];
    [NetHandleData getDataWithUrl:strAlbum completion:^(NSData *data) {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        self.firstSectionStr = [result objectForKey:@"totalCount"];
//        NSLog(@"%@", self.firstSectionStr);
//        self.secondSectionStr = [result objectForKey:@""]
        NSArray *tempArr = [result objectForKey:@"list"];
        self.albumArr = [NSMutableArray array];
        
        for (NSDictionary *dic in tempArr) {
            FindHotAlbum *album = [[FindHotAlbum alloc] initWithDictionary:dic];
            [self.albumArr addObject:album];
            [album release];
        }
//        NSLog(@"%@", [[self.albumArr lastObject] title]);
        [self.personTableView reloadData];
    }];
    // 请求3
    NSString *stateStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/1/30?device=iPhone", self.mayBeLikeUser.uid];
    
    [NetHandler getDataWithUrl:stateStr completion:^(NSData *data) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.secondSectionStr = [result objectForKey:@"totalCount"];
        
        NSMutableArray *tempArr = [result objectForKey:@"list"];
//        HotPresent *hott = [[HotPresent alloc]init];
//        self.hotPresentArr = [NSMutableArray arrayWithObjects:hott, nil];
        for (NSDictionary *dic in tempArr) {
            
            HotPresent *hot = [[HotPresent alloc] initWithDictionary:dic];
            [self.hotPresentArr addObject:hot];
//            NSLog(@"%@", hot);
            [hot release];
        }
        
        [self.personTableView reloadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self handleData];
    self.navigationController.navigationBarHidden = YES;
    // 播放键
    self.roundView = [JingRoundView defaultJingRoundView];
    self.roundView.frame = CGRectMake((self.view.bounds.size.width / 2) - 30, - 10, 60, 60);
    self.roundView.delegate = self;
    self.roundView.rotationDuration = 5.0;
    [self.backView addSubview:self.roundView];
}
// 偏移量大于一定程度就变色
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{   
    if (scrollView.contentOffset.y > self.view.bounds.size.height / 2) {
        self.topView.backgroundColor = [UIColor whiteColor];
    } else if (scrollView.contentOffset.y < self.view.bounds.size.height / 2) {
        self.topView.backgroundColor = [UIColor clearColor];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    self.view.backgroundColor = [UIColor whiteColor];
    [super.navigationController setNavigationBarHidden:NO];
    self.personTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 15) style:UITableViewStyleGrouped];
    [self.view addSubview:self.personTableView];
    self.personTableView.dataSource = self;
    self.personTableView.delegate = self;
    // 给一张背景照片
    UIImageView *back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableViewback.jpg"]];
    self.personTableView.backgroundView = back;
    back.userInteractionEnabled = YES;
    [back release];
    
    // 自定义tabbar
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width, 49)];
    [self.view addSubview:self.backView];
    self.backView.backgroundColor = [UIColor tabBarColor];
    // 返回键
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backView addSubview:backButton];
    backButton.frame = CGRectMake(10, 10, 30, 25);
    [backButton setImage:[UIImage imageNamed:@"iconfont-gerenfanhui.png"] forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor sectionColor]];
    [backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    
    [_personTableView release];
    
    // 背景图片
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 60 * 31)];
    self.headView.backgroundColor = [UIColor whiteColor];
    self.personBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -self.view.bounds.size.height / 80 * 23, self.view.bounds.size.width, self.view.bounds.size.height / 40 * 24)];
//    self.personBackgroundImageView.backgroundColor = [UIColor blueColor];
    [self.headView addSubview:self.personBackgroundImageView];
    self.personTableView.tableHeaderView = self.headView;
    UIView *alpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.personBackgroundImageView.frame.size.width, self.personBackgroundImageView.frame.size.height)];
    [self.personBackgroundImageView addSubview:alpView];
    self.personBackgroundImageView.userInteractionEnabled = YES;
    alpView.backgroundColor = [UIColor whiteColor];
    alpView.alpha = 0.7;
    // 网络请求下来的图片
//    [self.personBackgroundImageView setImageWithURL:[NSURL URLWithString:@"http://fdfs.xmcdn.com/group4/M08/3B/F6/wKgDs1QoN-nwAXGJAAHINRhPlX4330_large.jpg"]];
    [self.headView release];
    [_personBackgroundImageView release];
    [alpView release];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.personBackgroundImageView.bounds.size.height / 2, self.personBackgroundImageView.bounds.size.width, self.personBackgroundImageView.bounds.size.height / 2)];
    view.backgroundColor = [UIColor clearColor];
    [self.personBackgroundImageView addSubview:view];
    [view release];
    
    // 头像按钮
    self.personButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 8 * 3, 0, self.view.bounds.size.width / 4, self.view.bounds.size.width / 4)];
    [view addSubview:self.personButton];
//    self.personButton.backgroundColor = [UIColor yellowColor];
    [self.personButton addTarget:self action:@selector(personButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_personButton release];
    
    // 用户名
    self.personNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 4, self.view.bounds.size.width / 4 + 5, self.view.bounds.size.width / 2, 20)];
    [view addSubview:self.personNameLabel];
//    self.personNameLabel.backgroundColor = [UIColor magentaColor];
//    self.personNameLabel.text = @"NJ早安";
    self.personNameLabel.textAlignment = NSTextAlignmentCenter;
    self.personNameLabel.font = [UIFont systemFontOfSize:18];
    [_personNameLabel release];
    
    // 用户简介
    self.personDescribeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 8, self.view.bounds.size.width / 4 + 5 + 20, self.view.bounds.size.width / 4 * 3, view.bounds.size.height - (self.view.bounds.size.width / 4 + 5 + 20))];
    [view addSubview:self.personDescribeLabel];
//    self.personDescribeLabel.backgroundColor = [UIColor grayColor];
    self.personDescribeLabel.numberOfLines = 3;
    self.personDescribeLabel.textAlignment = NSTextAlignmentCenter;
//    self.personDescribeLabel.text = @"QQ听众交流信：NJzaoan303 新浪微博@NJ早安 QQ听众交流群 1群：227028824 QQ听众交流信：NJzaoan303 新浪微博@NJ早安 QQ听众交流群 1群：227028824 QQ听众交流群 2群：122009";
    self.personDescribeLabel.font = [UIFont systemFontOfSize:13];
    [_personDescribeLabel release];
//    UIView *threeButtonBackView = [[UIView alloc] initWithFrame:CGRectMake(0, self.personBackgroundImageView.frame.size.height / 2 + self.view.bounds.size.height / 30, self.view.bounds.size.width, 35)];
    // 关注按钮
    self.attentionButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3, self.personBackgroundImageView.frame.size.height / 2 + self.view.bounds.size.height / 30, self.view.bounds.size.width / 3, 35)];
    [self.headView addSubview:self.attentionButton];
    self.attentionButton.backgroundColor = [UIColor kuGouColor];
    [self.attentionButton setTitle:@"+ 关注" forState:UIControlStateNormal];
    [self.attentionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.attentionButton addTarget:self action:@selector(attentionButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_attentionButton release];
    
    // 已关注
    self.hadAttentionButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3, self.personBackgroundImageView.frame.size.height / 2 + self.view.bounds.size.height / 30, self.view.bounds.size.width / 3, 35)];
    self.hadAttentionButton.backgroundColor = [UIColor kuGouColor];
    [self.hadAttentionButton setTitle:@"已关注" forState:UIControlStateNormal];
    [self.hadAttentionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.hadAttentionButton addTarget:self action:@selector(hadAttentionButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_hadAttentionButton release];
    
    // 个人关注的人数按钮
    self.attentionCountButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.headView.bounds.size.height / 10 * 9, self.view.bounds.size.width / 3, self.headView.frame.size.height - self.headView.bounds.size.height / 10 * 9)];
//    self.attentionCountButton.backgroundColor = [UIColor cyanColor];
    [self.headView addSubview:self.attentionCountButton];
    [self.attentionCountButton setTitle:@"关注" forState:UIControlStateNormal];
    [self.attentionCountButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.attentionCountButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.attentionCountButton addTarget:self action:@selector(attentionCountButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_attentionCountButton release];
    
    self.attentionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.headView.frame.size.height - self.attentionCountButton.frame.size.height * 1.75, self.view.bounds.size.width / 3, self.attentionCountButton.frame.size.height / 4 * 3)];
    [self.headView addSubview:self.attentionLabel];
//    self.attentionLabel.backgroundColor = [UIColor blueColor];
    self.attentionLabel.textColor = [UIColor blackColor];
    self.attentionLabel.font = [UIFont systemFontOfSize:16];
//    self.attentionLabel.text = @"18";
    self.attentionLabel.textAlignment = NSTextAlignmentCenter;
    [_attentionLabel release];
    
    // 粉丝数按钮
    self.fansCountButton = [[UIButton alloc] initWithFrame:CGRectMake(1 + self.view.bounds.size.width / 3, self.headView.bounds.size.height / 10 * 9, - 1 + self.view.bounds.size.width / 3, self.headView.frame.size.height - self.headView.bounds.size.height / 10 * 9)];
//    self.fansCountButton.backgroundColor = [UIColor cyanColor];
    [self.headView addSubview:self.fansCountButton];
    [self.fansCountButton setTitle:@"粉丝" forState:UIControlStateNormal];
    [self.fansCountButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.fansCountButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.fansCountButton addTarget:self action:@selector(fansCountButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_fansCountButton release];
    
    self.fansLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.fansCountButton.frame.origin.x, self.headView.frame.size.height - self.fansCountButton.frame.size.height * 1.75, self.view.bounds.size.width / 3, self.fansCountButton.frame.size.height / 4 * 3)];
    [self.headView addSubview:self.fansLabel];
//    self.fansLabel.backgroundColor = [UIColor blueColor];
    self.fansLabel.textColor = [UIColor blackColor];
    self.fansLabel.font = [UIFont systemFontOfSize:16];
//    self.fansLabel.text = @"395.5万";
    self.fansLabel.textAlignment = NSTextAlignmentCenter;
    [_fansLabel release];
    
    // 赞过的按钮
    self.favouredCountButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 3 * 2, self.headView.bounds.size.height / 10 * 9, self.view.bounds.size.width / 3, self.headView.frame.size.height - self.headView.bounds.size.height / 10 * 9)];
//    self.favouredCountButton.backgroundColor = [UIColor cyanColor];
    [self.headView addSubview:self.favouredCountButton];
    [self.favouredCountButton setTitle:@"赞过的" forState:UIControlStateNormal];
    [self.favouredCountButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.favouredCountButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.favouredCountButton addTarget:self action:@selector(favouredCountButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_favouredCountButton release];
    
    self.favouredLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.favouredCountButton.frame.origin.x, self.headView.frame.size.height - self.favouredCountButton.frame.size.height * 1.75, self.view.bounds.size.width / 3, self.favouredCountButton.frame.size.height / 4 * 3)];
    [self.headView addSubview:self.favouredLabel];
//    self.favouredLabel.backgroundColor = [UIColor blueColor];
    self.favouredLabel.textColor = [UIColor blackColor];
    self.favouredLabel.font = [UIFont systemFontOfSize:16];
//    self.favouredLabel.text = @"9万";
    self.favouredLabel.textAlignment = NSTextAlignmentCenter;
    [_favouredLabel release];
    
    
    
    
//    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
//    [self.view addSubview:self.topView];

//    // 自定义tabbar
//    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width, 49)];
//    [self.view addSubview:backView];
//    backView.backgroundColor = [UIColor tabBarColor];
//    // 返回键
//    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [backView addSubview:backButton];
//    backButton.frame = CGRectMake(10, 10, 30, 25);
//    [backButton setImage:[UIImage imageNamed:@"iconfont-gerenfanhui.png"] forState:UIControlStateNormal];
//    [backButton setTintColor:[UIColor sectionColor]];
//    [backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//    // 播放键
//    self.roundView = [JingRoundView defaultJingRoundView];
//    self.roundView.frame = CGRectMake((self.view.bounds.size.width / 2) - 30, - 10, 60, 60);
//    self.roundView.delegate = self;
//    self.roundView.rotationDuration = 5.0;
//    [backView addSubview:self.roundView];

    
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
    [self.view addSubview:self.topView];
    
    [self setupRefresh];
    [_topView release];
    
    
}
// 自定义tabbar中播放按钮方法
-(void)playStatuUpdate:(BOOL)playState
{
    NSLog(@"%@...", playState ? @"播放": @"暂停了");
    if (playState == YES) {
        NSLog(@"播放");
        AvReadioPlayer *avPlayer = [AvReadioPlayer defaultAvPlayer];
        [avPlayer play];
    } else {
        NSLog(@"暂停");
        AvReadioPlayer *avPlayer = [AvReadioPlayer defaultAvPlayer];
        [avPlayer pause];
        
    }
}
- (void)backButtonClicked:(UIButton *)button
{
    NSLog(@"返回上一页");
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)personButtonClicked:(UIButton *)button
{
    NSLog(@"头像");
}
- (void)attentionButtonClicked:(UIButton *)button
{
    NSLog(@"+关注");
    [self.headView addSubview:self.hadAttentionButton];
}
- (void)hadAttentionButtonClicked:(UIButton *)button
{
    NSLog(@"已关注");
    [self.headView addSubview:self.attentionButton];
}
- (void)attentionCountButtonClicked:(UIButton *)button
{
    NSLog(@"关注");
}
- (void)fansCountButtonClicked:(UIButton *)button
{
    NSLog(@"粉丝");
}
- (void)favouredCountButtonClicked:(UIButton *)button
{
    NSLog(@"赞过的");
}
// 自定义section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *firstSectionView = [[UIView alloc] init];
        firstSectionView.backgroundColor = [UIColor clearColor];
        UILabel *firstSectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width - 10, 42)];
        [firstSectionView addSubview:firstSectionLabel];
        firstSectionLabel.textColor = [UIColor sectionColor];
//        firstSectionLabel.backgroundColor = [UIColor clearColor];
        firstSectionLabel.font = [UIFont systemFontOfSize:14];
        firstSectionLabel.text = [NSString stringWithFormat:@"发布的专辑  (%@)", self.firstSectionStr];
        return firstSectionView;
    } else if (section == 1) {
        UIView *secondSectionView = [[UIView alloc] init];
        secondSectionView.backgroundColor = [UIColor clearColor];
        UILabel *secondSectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width - 10, 42)];
        [secondSectionView addSubview:secondSectionLabel];
        secondSectionLabel.textColor = [UIColor sectionColor];
        secondSectionLabel.backgroundColor = [UIColor clearColor];
        secondSectionLabel.font = [UIFont systemFontOfSize:14];
        secondSectionLabel.text = [NSString stringWithFormat:@"发布的声音  (%@)", self.secondSectionStr];
        return secondSectionView;
    } else {
        return nil;
    }
}
// footer 高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 30;
    } else {
        return 20;
    }
}

 // 自定义footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *ButtonView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 19)] autorelease];
        UIButton *lookAllButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [ButtonView addSubview:lookAllButton];
        lookAllButton.frame = CGRectMake(0, 0, ButtonView.bounds.size.width, ButtonView.bounds.size.height);
        [lookAllButton setTitle:@"查看全部专辑 >" forState:UIControlStateNormal];
        lookAllButton.backgroundColor = [UIColor whiteColor];
        [lookAllButton setTitleColor:[UIColor kuGouColor] forState:UIControlStateSelected];
//        [lookAllButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [lookAllButton addTarget:self action:@selector(lookAllButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        return ButtonView;
    } else {
        UIView *threeButtonView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 19)] autorelease];
        // "赞"
        self.favourButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, threeButtonView.frame.size.width / 3, threeButtonView.frame.size.height)];
        self.click = NO;
        [threeButtonView addSubview:self.favourButton];
        [self.favourButton setTitle:@"赞" forState:UIControlStateNormal];
        self.favourButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.favourButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.favourButton.backgroundColor = [UIColor buttonBackColor];
        [self.favourButton addTarget:self action:@selector(favourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_favourButton release];
        
        // "分享"
        self.talkButton = [[UIButton alloc] initWithFrame:CGRectMake(1 + threeButtonView.frame.size.width / 3, 0, threeButtonView.frame.size.width / 3, threeButtonView.frame.size.height)];
        self.talkButton.tag = section + 1000;
        [threeButtonView addSubview:self.talkButton];
        [self.talkButton setTitle:@"分享" forState:UIControlStateNormal];
        self.talkButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.talkButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.talkButton.backgroundColor = [UIColor buttonBackColor];
        [self.talkButton addTarget:self action:@selector(talkButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_talkButton release];
        
        
        // "下载"
        self.downLoadButton = [[UIButton alloc] initWithFrame:CGRectMake(2 + threeButtonView.frame.size.width / 3 * 2, 0, threeButtonView.frame.size.width / 3, threeButtonView.frame.size.height)];
        [threeButtonView addSubview:self.downLoadButton];
        self.downLoadButton.tag = section;
        [self.downLoadButton setTitle:@"下载" forState:UIControlStateNormal];
        self.downLoadButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.downLoadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.downLoadButton.backgroundColor = [UIColor buttonBackColor];
        [self.downLoadButton addTarget:self action:@selector(downLoadButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_downLoadButton release];
        return threeButtonView;
    }
}
- (void)lookAllButtonClicked:(UIButton *)button
{
    NSLog(@"查看全部专辑");
    
    AllAlbumViewController *allVC = [[AllAlbumViewController alloc] init];
    allVC.mayBeLike = self.mayBeLikeUser;
    
    [self.navigationController pushViewController:allVC animated:YES];
    [allVC release];
    
}
- (void)favourButtonClicked:(UIButton *)button
{
    if (self.click == NO) {
        [button setTitle:@"已赞" forState:UIControlStateNormal];
        NSLog(@"已赞");
    } else {
        [button setTitle:@"赞" forState:UIControlStateNormal];
        NSLog(@"赞");
    }
    self.click = !self.click;
}
- (void)talkButtonClicked:(UIButton *)button
{
    NSLog(@"分享");
    AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = REDIRECTURI;
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare:button.tag] authInfo:authRequest access_token:myDelegate.wbtoken];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    [WeiboSDK sendRequest:request];
}
- (WBMessageObject *)messageToShare:(NSInteger)tag
{
    HotPresent *focusFetaill = [self.hotPresentArr objectAtIndex:tag - 1000];
    WBMessageObject *message = [WBMessageObject message];
    NSString *str = [NSString stringWithFormat:@"我使用悦享听分享了一条信息:\n%@\n%@", focusFetaill.title, focusFetaill.playUrl64];
    message.text = str;
    WBImageObject *image = [WBImageObject object];
    image.imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:focusFetaill.coverSmall]];
    message.imageObject = image;
    return message;
}
- (void)downLoadButtonClicked:(UIButton *)button
{
        NSLog(@"下载%ld", (long)button.tag);
        HotPresent *hot = [self.hotPresentArr objectAtIndex:button.tag];
        NSLog(@"下载内容:%@, 下载地址:%@", hot.title, hot.downloadUrl);
        NSString *fileName = [hot.title stringByAppendingFormat:@".aac"];
        [[FilesDownManage sharedFilesDownManage]downFileUrl:hot.downloadUrl filename:fileName filetarget:@"aac" fileimage:nil];
        NSLog(@"%@", [FilesDownManage sharedFilesDownManage].downinglist);
    
}
 //section 高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 42;
    } else if (section == 1) {
        return 42;
    } else {
        return 0;
    }
}
// section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.hotPresentArr.count;
}
// cell 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.albumArr.count;
    } else {
        return 1;
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor whiteColor];
}
// cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSString *title = [[self.albumArr objectAtIndex:indexPath.row] title];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        
        CGRect rect = [title boundingRectWithSize:CGSizeMake(self.personTableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + self.view.bounds.size.height / 667 * 40;
    } else {
        NSString *title = [[self.hotPresentArr objectAtIndex:indexPath.section] title];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect = [title boundingRectWithSize:CGSizeMake(self.personTableView.bounds.size.width / 2, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        CGFloat height = rect.size.height > (kCellWidth / 6) ? rect.size.height : (kCellWidth / 6);
        return height + 55;
    }
}
// 给cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *str = @"reuse";
        DownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[DownloadCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
        
        cell.findAlbum = [self.albumArr objectAtIndex:indexPath.row];
        return cell;
    } else {
        
        static NSString *str = @"hotReuse";
        HotCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[HotCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
//        if (indexPath.section == 1 && indexPath.row == 0) {
//            cell.hotPre = [self.hotPresentArr objectAtIndex:0];
//            NSLog(@"%@", [[self.hotPresentArr objectAtIndex:indexPath.section] title]);
//            return cell;
//        }
        cell.hotPre = [self.hotPresentArr objectAtIndex:indexPath.section];
//        NSLog(@"%@", [[self.hotPresentArr objectAtIndex:indexPath.section] title]);
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        AlbumViewController *albumVC = [[AlbumViewController alloc] init];
        [self.navigationController pushViewController:albumVC animated:YES];
        
        AlbumTracks *tracks = [[AlbumTracks alloc] init];
        tracks = [self.albumArr objectAtIndex:indexPath.row];
        
        ClassifyDetail *detail = [[ClassifyDetail alloc] init];
        detail.pageId = tracks.albumId;
        albumVC.classifyDetail = detail;
        [detail release];
        [albumVC release];
        
    } else {
        self.hotP = [self.hotPresentArr objectAtIndex:indexPath.section];
//        NSLog(@"%ld", self.hotPresentArr.count);
        PlayViewController *playVC = [PlayViewController defaultPlayVC];
        if (playVC.albumArr.count != 0) {
            [playVC.albumArr removeAllObjects];
        }
        playVC.hotPlayArr = [NSMutableArray arrayWithArray:self.hotPresentArr];
//        NSLog(@"%@", playVC.hotPlayArr);
        playVC.index = indexPath.section;
//        NSLog(@"%ld", playVC.index);
        playVC.hotPlay = self.hotP;
//        NSLog(@"%@", playVC.hotPlay);
        [self presentViewController:playVC animated:YES completion:^{
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)handleFooterData
{
    self.page = kPage + self.page;
    // 请求3
    NSString *stateStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/%ld/30?device=iPhone", self.mayBeLikeUser.uid, self.page];
    
    [NetHandler getDataWithUrl:stateStr completion:^(NSData *data) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.secondSectionStr = [result objectForKey:@"totalCount"];
        
        NSMutableArray *tempArr = [result objectForKey:@"list"];
//        HotPresent *hott = [[HotPresent alloc]init];
//        self.hotPresentArr = [NSMutableArray arrayWithObjects:hott, nil];
        for (NSDictionary *dic in tempArr) {
            
            HotPresent *hot = [[HotPresent alloc] initWithDictionary:dic];
            [self.hotPresentArr addObject:hot];
            //            NSLog(@"%@", hot);
            [hot release];
        }
        
        [self.personTableView reloadData];
    }];

}

- (void)setupRefresh
{
    //    NSLog(@"进入了setupRefresh");
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    
//    [self.personTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    //进入刷新状态(一进入程序就下拉刷新)
//    [self.personTableView headerBeginRefreshing];
    
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.personTableView addFooterWithTarget:self action:@selector(footerRereshing)];
}



#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    //1. 在这调用请求网络数据方法（刷新数据）
    
    [self handleData];
    
    
    //2. 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.personTableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.personTableView headerEndRefreshing];
    });
}

//   进入加载状态
- (void)footerRereshing
{
    //1. 拼接口等操作
    // 请求加载数据
    
    [self handleFooterData];
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.personTableView reloadData];
        [self.personTableView footerEndRefreshing];
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
